//
//  CarNewInfoCell.m
//  STool
//
//  Created by Samuel on 15/9/8.
//
//

#import "CarNewInfoCell.h"
#import "SAlertView.h"
#import "SCoreData.h"
@implementation CarNewInfoCell
{
    UIButton *rightButton;
}

- (void)awakeFromNib {
    self.timeOfCarBuy.dateDelegate = self;
    self.timeOfCarBuy.layer.masksToBounds = YES;
    self.timeOfCarBuy.layer.cornerRadius = 3;
    self.timeOfCarBuy.layer.borderWidth = .5;
    self.timeOfCarBuy.layer.borderColor = kUIColorFromRGB(0x829FF4).CGColor;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    [self.carStyle becomeFirstResponder];
    self.delegate.rightButton.selected = YES;
    
}

- (IBAction)confirmButton:(UIButton *)sender {
    if ([self.carStyle.text isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"请填写车型"];
        return;
    }
    if ([self.hundredKMExpend.text isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"请填写百公里平均油耗"];
        return;
    }
    if ([self.gasolineNumber.text isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"请填写加油油号"];
        return;
    }
    if ([self.timeOfCarBuy.text isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"请填写购买时间"];
        return;
    }
    
    SCoreData *coreData = [[SCoreData sharedInstance]setTableName:@"CarInfo" andDatabaseName:@"CarDB"];
    NSMutableDictionary *saveData = [[NSMutableDictionary alloc]init];
    [saveData setObject:self.carStyle.text forKey:@"carType"];
    [saveData setObject:[NSString stringWithFormat:@"%.f",[[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970]] forKey:@"carId"];
    [saveData setObject:self.hundredKMExpend.text forKey:@"glCost"];
    [saveData setObject:self.gasolineNumber.text forKey:@"glType"];
    [saveData setObject:self.timeOfCarBuy.timestamp forKey:@"carBuyTime"];
    BOOL isInsertOk = [coreData insertCoreData:saveData andKey:@"carId", nil];
    if (isInsertOk) {
        [self.delegate tableViewRemoveObjectAtIndexZero];
        [self.delegate getCarDatas];
        return;
    }
    [SAlertView showSAlterViewTitle:@"警告" Message:@"本地数据写入失败"];
}
- (void)cleanTextDatas;
{
    self.carStyle.text = self.hundredKMExpend.text = self.gasolineNumber.text = self.timeOfCarBuy.text = @"";
}
@end
